package com.airena.controller;

import com.airena.entity.dto.RoleInfoDTO;
import com.airena.entity.dto.RoleInfoPageQueryDTO;
import com.airena.entity.po.RoleInfo;
import com.airena.result.PageResult;
import com.airena.result.Result;
import com.airena.service.IRoleInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 实现角色相关接口
 */
@RestController
@Tag(name = "角色接口")
@RequestMapping(("/roleInfo"))
@Slf4j
public class RoleController {

    @Autowired
    private IRoleInfoService roleInfoService;


    /**
     * 根据id获取角色信息
     *
     * @param roleId 角色id
     * @return 角色信息
     */
    @GetMapping("/{roleId}")
    @Operation(summary = "根据id获取角色信息")
    public Result<RoleInfo> getRoleInfo(@PathVariable Long roleId) {
        log.info("根据id获取角色信息{}", roleId);
        return Result.success(roleInfoService.getRoleInfo(roleId));
    }

    /**
     * 分页查询角色信息
     * @param pageQueryDTO
     * @return
     */
    @GetMapping
    @Operation(summary = "分页查询角色信息")
    public Result<PageResult> queryRolePage(@ParameterObject RoleInfoPageQueryDTO pageQueryDTO) {
        log.info("分页查询角色信息{}", pageQueryDTO);
        Page<RoleInfo> page = roleInfoService.queryRolePage(pageQueryDTO);
        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }

//    TODO 增角色信息

    /**
     * 新增角色信息
     * @param roleInfoDTO
     * @return
     */
    @PostMapping
    @Operation(summary = "新增角色信息")
    public Result addRoleInfo(@ParameterObject @Validated RoleInfoDTO roleInfoDTO) {
        log.info("新增角色信息{}", roleInfoDTO);
        roleInfoService.addRoleInfo(roleInfoDTO);
        return Result.success();
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @DeleteMapping
    @Operation(summary = "删除角色信息")
    public Result deleteRoleInfo(Long roleId) {
        log.info("删除角色信息{}", roleId);
        return Result.success(roleInfoService.deleteRoleInfo(roleId));
    }

    /**
     * 修改角色信息
     * @param roleInfoDTO
     * @return
     */
    @PutMapping
    @Operation(summary = "修改角色信息")
    public Result updateRoleInfo(@ParameterObject RoleInfoDTO roleInfoDTO) {
        log.info("修改角色信息{}", roleInfoDTO);
        return Result.success(roleInfoService.updateRoleInfo(roleInfoDTO));
    }


}
